package com.example.administrator.testpersionn.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.administrator.testpersionn.R;
import com.example.administrator.out1.bena.ListBean;
import com.example.administrator.out1.callback.ShopItemCallBack;
import com.example.administrator.out1.view.ShoppingCartListView;

import java.util.List;
import java.util.Vector;

public class ListAdapter extends BaseAdapter {
    List<ListBean> listBeans;
    Context context;
    private int lastPosition = -1;   //lastPosition 记录上一次选中的图片位置，-1表示未选中
    private Vector<Boolean> vector = new Vector<Boolean>();
    ShopItemCallBack callBack;
    public ListAdapter(List<ListBean> listBeans, Context context) {
        this.listBeans = listBeans;
        this.context = context;
        lastPosition=0;
        vector.add(0,true);
        for (int i = 1; i < listBeans.size(); i++) {
            vector.add(false);
        }
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            viewHolder=new ViewHolder();
            convertView=LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);
            viewHolder.listView=convertView.findViewById(R.id.itemList);
            viewHolder.textView=convertView.findViewById(R.id.itemText);
            convertView.setTag(viewHolder);

        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        final ListBean listBean = listBeans.get(position);
        viewHolder.textView.setText(listBean.getName());
        viewHolder.listView.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,listBean.getList()));
        if (vector.elementAt(position) == true) {
            viewHolder.listView.setVisibility(View.VISIBLE);
        } else {
            viewHolder.listView.setVisibility(View.GONE);
        }
        viewHolder.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                callBack.item(listBean.getList().get(position));
            }
        });

        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeState(position);
                callBack.item(listBean.getName());
            }
        });


        return convertView;
    }
    public void setOnitem(ShopItemCallBack callBack){
        this.callBack=callBack;
    }

    class ViewHolder{
        TextView textView;
        ShoppingCartListView listView;
    }
    public void changeState(int pos) {
        if(lastPosition != -1)
            vector.setElementAt(false, lastPosition);                   //取消上一次的选中状态
        vector.setElementAt(!vector.elementAt(pos), pos);     //直接取反即可
        lastPosition = pos;                                        //记录本次选中的位置
        notifyDataSetChanged();                                         //通知适配器进行更新
    }
}
